/**
 * Copyright (C) 2013-2014 the original author or authors.
 */
package com.foreveross.bsl.system.application.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.inject.Named;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;

import com.foreveross.bsl.common.utils.bean.BeanHelper;
import com.foreveross.bsl.system.application.RouterClassDatabaseApplication;
import com.foreveross.bsl.system.application.vo.RouterClassDatabaseVO;
import com.foreveross.bsl.system.model.domain.RouterConnection;

/**
 * @description
 *
 * @author rening
 * @version v1.0
 *
 * @2013-6-7
 *
 */
@Named("routerClassDatabaseApplication")
public class RouterClassDatabaseApplicationImpl implements RouterClassDatabaseApplication {

	/* (non-Javadoc)
	 * @see com.foreveross.bsl.system.application.DatabaseInfoApplication#saveEntity(com.foreveross.bsl.system.application.vo.DatabaseInfoVO)
	 */
	@Override
	public RouterClassDatabaseVO saveEntity(RouterClassDatabaseVO entity) {
		RouterConnection RouterClassDatabase = new RouterConnection();
		BeanHelper.copyProperties(RouterClassDatabase, entity);
		RouterClassDatabase.save();
		entity.setId(RouterClassDatabase.getId());
		return entity;
	}

	/* (non-Javadoc)
	 * @see com.foreveross.bsl.system.application.DatabaseInfoApplication#updateEntity(com.foreveross.bsl.system.application.vo.DatabaseInfoVO)
	 */
	@Override
	public RouterClassDatabaseVO updateEntity(RouterClassDatabaseVO entity) {
		RouterConnection routerClassDatabase = new RouterConnection();
		BeanHelper.copyProperties(routerClassDatabase, entity);
		routerClassDatabase.save();
		BeanHelper.copyProperties(entity, routerClassDatabase);
		return entity;
	}

	/* (non-Javadoc)
	 * @see com.foreveross.bsl.system.application.DatabaseInfoApplication#deleteEntity(com.foreveross.bsl.system.application.vo.DatabaseInfoVO)
	 */
	@Override
	public void deleteEntity(RouterClassDatabaseVO entity) {
		RouterConnection routerClassDatabase = new RouterConnection();
		BeanHelper.copyProperties(routerClassDatabase, entity);
		routerClassDatabase.delete();
	}

	/* (non-Javadoc)
	 * @see com.foreveross.bsl.system.application.DatabaseInfoApplication#getEntity(java.lang.String)
	 */
	@Override
	public RouterClassDatabaseVO getEntity(String id) {
		RouterConnection routerClassDatabase = new RouterConnection();
		routerClassDatabase = (RouterConnection)routerClassDatabase.findOne(id);
		RouterClassDatabaseVO vo = new RouterClassDatabaseVO();
		BeanHelper.copyProperties(vo, routerClassDatabase);
		return vo;
	}

	/* (non-Javadoc)
	 * @see com.foreveross.bsl.system.application.DatabaseInfoApplication#findAll()
	 */
	@Override
	public List<RouterClassDatabaseVO> findAll() {
		RouterConnection routerClassDatabase = new RouterConnection();
		Iterable iterable = routerClassDatabase.findAll();
		if(iterable != null){
			List<RouterClassDatabaseVO> result = new ArrayList<RouterClassDatabaseVO>();
			RouterClassDatabaseVO vo;
			for (Iterator iterator = iterable.iterator(); iterator.hasNext();) {
				RouterConnection obj = (RouterConnection) iterator.next();

				vo = new RouterClassDatabaseVO();
				BeanHelper.copyProperties(vo, obj);
				result.add(vo);
			}
			return result;
		}
		return null;
	}

	/* (non-Javadoc)
	 * @see com.foreveross.bsl.system.application.DatabaseInfoApplication#findByPageable(org.springframework.data.domain.Pageable)
	 */
	@Override
	public Page<RouterClassDatabaseVO> findByPageable(Pageable pageable) {
		RouterConnection routerClassDatabase = new RouterConnection();
		Page page = routerClassDatabase.findAll(pageable);
		if(page != null && page.getContent() != null){
			List<RouterClassDatabaseVO> result = new ArrayList<RouterClassDatabaseVO>();
			RouterClassDatabaseVO vo;
			for (Iterator iterator = page.getContent().iterator(); iterator.hasNext();) {
				RouterConnection obj = (RouterConnection) iterator.next();

				vo = new RouterClassDatabaseVO();
				BeanHelper.copyProperties(vo, obj);
				result.add(vo);
			}
			Page<RouterClassDatabaseVO> pageVO = new PageImpl<RouterClassDatabaseVO>(result, pageable, page.getTotalElements());
			return pageVO;
		}
		return null;
	}

}
